/**
 * Create by CaoWenjian
 *
 * @copyRight: 2018
 **/
package com.bjboot.caocao.system.listener;

import org.apache.shiro.session.Session;
import org.apache.shiro.session.SessionListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.atomic.AtomicInteger;

/**
 *@Description 系统session监听器
 *@Author 操文健
 *@Date 2018-09-06 下午12:48
 *@Version 1.0
 **/
public class SysSessionListener implements SessionListener {

    private final Logger logger = LoggerFactory.getLogger(SysSessionListener.class);
    private final AtomicInteger sessionCount = new AtomicInteger(0);
    @Override
    public void onStart(Session session) {
        logger.info("有新的客户端连接，session对应Id：" + session.getId());
        sessionCount.incrementAndGet();
    }

    @Override
    public void onStop(Session session) {
        logger.info("人工登出Session的Id为[" + session.getId() + "]销毁成功...");
        sessionCount.decrementAndGet();
    }

    @Override
    public void onExpiration(Session session) {
        logger.info("Session为[" + session.getId() + "]的连接到期失效...");
        sessionCount.decrementAndGet();
    }

    /**
     * 获取session数量
     * @return
     */
    public int getSessionCount() {
        return sessionCount.get();
    }
}
